<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: ewgof
 * @Date: 2021-04-28 16:43:47
 * @LastEditors: ewgof
 * @LastEditTime: 2022-05-23 10:07:39
 */
$config_queue = require __DIR__ . '/config.php';
global $config_queue;

if (!function_exists('logw')) {
    /**
     * @name:
     * @msg: 写日志函数
     * @param {string} $msg 日志内容
     * @param {string} $level 日志级别
     * @return {*}
     */
    function logw(string $msg, string $level = 'info')
    {
        global $config_queue;
        $msg = '----->>> | ' . date('Y-m-d H:i:s') . ' | [ ' . $level . ' ] | ' . $msg;
        $path = $config_queue['log']['path'];
        // var_dump($path);
        if (!file_exists($path)) {
            mkdir($path, 0755, true);
        }
        $file = $path . '/' . $level . '_' . $config_queue['log']['filename'];
        file_put_contents($file, $msg, FILE_APPEND);
    }
}


if (!function_exists('getQueueDriver')) {
    /**
     * @name: 
     * @msg: 获取驱动类
     * @param {*}
     * @return {*}
     */
    function getQueueDriver()
    {
        global $config_queue;
        return $config_queue['default']['driver_class'];
    }
}


if (!function_exists('generateMessageId')) {
    /**
     * @name: 
     * @msg: 生成消息 id
     * @param {*}
     * @return {*}
     */
    function generateMessageId(string $prefix = '') {
        if (!empty($prefix)) {
            $prefix = strtoupper($prefix);
        }
        $rand_str = uniqid() . mt_rand(1, 99999) . microtime(true);
        $id = $prefix . hash_hmac('sha256', $rand_str, substr($rand_str, 3, 6));
        return $id;
    }
}